package com.abe.libquick.main.video.record;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;

import com.abe.libcore.base.screen.OnHandlerListener;
import com.abe.libcore.utils.FileUtils;
import com.abe.libquick.R;
import com.abe.libquick.R2;
import com.abe.libquick.main.video.player.VideoRecordHelper;
import com.abe.libquick.utils.NormalUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class VideoRecordActivity extends VideoBaseActivity implements SurfaceHolder.Callback, Animator.AnimatorListener,
        MediaRecorder.OnErrorListener, OnHandlerListener {

    @BindView(R2.id.confirm)
    Button confirm;
    @BindView(R2.id.cancel)
    Button cancel;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_video_record;
    }

    private void showMenuButton() {
        float distance = (float) NormalUtils.getScreenWidth(getApplicationContext()) / 2 - 90 - 60;
        PropertyValuesHolder valuesHolder_left = PropertyValuesHolder.ofFloat(
                "translationX", distance, 0);
        PropertyValuesHolder valuesHolder_right = PropertyValuesHolder.ofFloat(
                "translationX", -distance, 0);
        PropertyValuesHolder valuesHolder_alpha = PropertyValuesHolder.ofFloat(
                "alpha", 0f, 0.2f, 0.4f, 1.0F);
        confirm.setVisibility(View.VISIBLE);
        cancel.setVisibility(View.VISIBLE);
        ObjectAnimator
                .ofPropertyValuesHolder(cancel, valuesHolder_left,
                        valuesHolder_alpha).setDuration(1000).start();
        ObjectAnimator
                .ofPropertyValuesHolder(confirm, valuesHolder_right,
                        valuesHolder_alpha).setDuration(1000).start();
    }

    @OnClick(R2.id.cancel)
    public void cancel() {
        setResultCancel();
    }

    @OnClick(R2.id.confirm)
    public void confirm() {
        Bundle bundle = new Bundle();
        bundle.putString(VideoRecordHelper.VIDEO_PATH, currentVideo);
        setResultOK(bundle);
    }

    @Override
    public void dispatchHandleMessage(Message msg) {
        switch (msg.what) {
            case REFRESH_PROGRESS:
                int progress = (int) ((float) count * 100 / (float) maxVideoLength);
                /* 刷新进度条 */
                if (progress == 0) {
                    progressBar.setProgress(0);
                } else {
                    progressBar.setProgressWithAnimation(progress, (long) 500);
                }
                break;
            case STOP_RECORD: {
                if (count < 15) {
                    animator_normal.start();
                } else {
                    /* 停止录制 */
                    control.setBackgroundResource(R.color.translate);
                    control.setVisibility(View.INVISIBLE);
                    btnStartStop.setVisibility(View.INVISIBLE);
                    progressBar.setVisibility(View.INVISIBLE);
                    showMenuButton();
                }
            }
            break;
            case RESET_RECORD: {
                /* 清除录制缓存 */
                FileUtils.INSTANCE.deleteFile(currentVideo);
                currentVideo = "";
                /* 初始化菜单 */
                progressBar.setProgress(0);
                control.setBackgroundResource(R.color.translate);
                control.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.INVISIBLE);
                btnStartStop.setVisibility(View.VISIBLE);
                confirm.setVisibility(View.GONE);
                cancel.setVisibility(View.GONE);
                if (mSurfaceHolder != null)
                    surfaceCreated(mSurfaceHolder);
            }
            break;
        }
    }
}